import { Service } from 'egg';

export default class PassportService extends Service {
  async login({ name, id }) {
    await this.ctx.login({ username: name, id });
    // 必须刷新 CSRF token, 参见: https://eggjs.org/zh-cn/core/security.html#%E5%88%B7%E6%96%B0-csrf-token
    this.ctx.rotateCsrfSecret();
  }

  async accountVerify(username, password) {
    const user = await this.ctx.model.AuthUser.findOne({ 
      where: {
        username
      }
     });

    // 用户不存在
    if (!user) {
      return { code: 10003 };
    }
    // 密码错误
    if (password !== user.password) {
      return { code: 10001, data: { } };
    }
    return { code: 0 };
  }
}
